package com.millstein.controller;

import com.millstein.request.CartItemRequest;
import com.millstein.service.CartService;
import com.millstein.util.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: tsing
 * @create: 2021-09-16 22:43
 */
@Api("购物车控制器")
@RestController
@RequestMapping("/api/cart/v1")
public class CartController {

    @Autowired
    private CartService cartService;

    @PostMapping("/add_cart")
    @ApiOperation("添加购物车")
    public JsonData addCartItem(@ApiParam("添加购物车所需要的数据") @RequestBody CartItemRequest cartItemRequest) {
        cartService.addCartItem(cartItemRequest);
        return JsonData.buildSuccess();
    }

    @PostMapping("/clear_cart")
    @ApiOperation("清空购物车")
    public JsonData clearCart() {
        cartService.clearCart();
        return JsonData.buildSuccess();
    }

    @PostMapping("/delete_cart/{product_id}")
    @ApiOperation("删除购物项")
    public JsonData deleteCartItem(@ApiParam("商品id") @PathVariable("product_id") Long productId) {
        cartService.deleteCartItem(productId);
        return JsonData.buildSuccess();
    }

    @PostMapping("/change_num")
    @ApiOperation("修改购物项数量")
    public JsonData changeCartItemNumber(@ApiParam("修改购物项所需要的数据") @RequestBody CartItemRequest cartItemRequest) {
        cartService.changeCartItemNumber(cartItemRequest);
        return JsonData.buildSuccess();
    }

    @GetMapping("my_cart")
    @ApiOperation("得到当前用户的购物车信息")
    public JsonData getCurrentCart() {
        return JsonData.buildSuccess(cartService.getCurrentCart());
    }

    @GetMapping("/getProductFromCart")
    @ApiOperation("从购物车中获取商品数据")
    JsonData getProductFromCart(@ApiParam("生成订单商品id的集合") @RequestParam("productIds") List<Long> productIds) {
        return JsonData.buildSuccess(cartService.getProductFromCart(productIds));
    }
}
